/*
 * Copyright (C) 2011 Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * pn_Disks.java
 *
 * Created on 23/09/2011, 03:08:19
 */
package kuasar.plugin.servermanager.gui.infoserver;

import java.awt.Component;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import kuasar.plugin.Intercom.GUI;
import kuasar.plugin.utils.Connection;
import kuasar.plugin.utils.SSocketTools;
import kuasar.plugin.utils.Server;
import kuasar.plugin.utils.pn_Info;

/**
 *
 * @author Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 */
public class pn_Disks extends kuasar.plugin.classMod.AbstractPanel{

    private DefaultListModel model = new DefaultListModel();
    private JPanel parent;
    private String address, ks, user;
    private char[] kspwd, userpwd;
    private int port;

    public pn_Disks(JPanel parent, String address, int port, String ks, char[] kspwd, String user, char[] userpwd) {
        this.parent=parent;
        this.address = address;
        this.port=port;
        this.ks=ks;
        this.user=user;
        this.kspwd=kspwd;
        this.userpwd=userpwd;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        spn_Disks = new javax.swing.JScrollPane();
        lst_Disks = new javax.swing.JList();
        btn_Back = new javax.swing.JButton();

        setOpaque(false);

        spn_Disks.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        spn_Disks.setViewportBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        spn_Disks.setOpaque(false);

        lst_Disks.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lst_Disks.setModel(model);
        lst_Disks.setOpaque(false);
        spn_Disks.setViewportView(lst_Disks);
        lst_Disks.setCellRenderer(new cellRenderer());

        btn_Back.setBackground(new java.awt.Color(0, 0, 0));
        btn_Back.setForeground(new java.awt.Color(204, 204, 204));
        btn_Back.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/servermanager/icons/previous16.png"))); // NOI18N
        btn_Back.setBorderPainted(false);
        btn_Back.setContentAreaFilled(false);
        btn_Back.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Back.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/servermanager/icons/previous16.png"))); // NOI18N
        btn_Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_BackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(spn_Disks, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btn_Back, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(12, 12, 12))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btn_Back, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spn_Disks, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                .addGap(12, 12, 12))
        );

        spn_Disks.getViewport().setOpaque(false);
    }// </editor-fold>//GEN-END:initComponents

    private void btn_BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_BackActionPerformed
        goBack();
    }//GEN-LAST:event_btn_BackActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Back;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lst_Disks;
    private javax.swing.JScrollPane spn_Disks;
    // End of variables declaration//GEN-END:variables

    private void goBack() {
        JPanel cont = (JPanel) this.getParent();
        parent.setBounds(0, 0, cont.getWidth(), cont.getHeight());
        cont.removeAll();
        cont.add(parent);
        cont.updateUI();
    }
    
    public void loadData(){
        SSocketTools st = startConnection();
        if(st==null)return;
        int tInfo = Server.getFSSize(st);
        for(int i =0; i<tInfo; i++){
            String data[] = new String[6];
            data[0]=Server.getFSPath(st, i);
            data[1]=Integer.toString(Server.getFSPercentUsed(st, i));
            data[2]=Server.getFSType(st, i);
            data[3]=Server.getFSTotal(st, i);
            data[4]=Server.getFSFree(st, i);
            data[5]=Server.getFSUsed(st, i);
            model.addElement(data.clone());
        }
        Server.closeServer(st);
        st.closeAll();
    }
    
    private SSocketTools startConnection(){
        SSocketTools st = new SSocketTools(address, port, ks, kspwd, user, userpwd);
        if (!st.initSocket()) {
            int status = Connection.checkServer(address, port, ks, kspwd, user, userpwd, false);
            pn_Info.Load((JPanel) this.getParent(), this, "Error connecting to sercer",
                    "Impossible connect to the server <p> Message error :: " + Connection.getErrorDescription(status),
                    pn_Info.ICON_ERROR);
            return null;
        }
        if (!Server.startConnection(st)) {
            pn_Info.Load((JPanel) this.getParent(), this, "Error connecting to server",
                    "It was impossible to connect to the server", pn_Info.ICON_ERROR);
            st.closeAll();
            return null;
        }
        return st;
    }

}
class cellRenderer implements ListCellRenderer{

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        String[] values = (String[]) value;
        pn_ListCellRendererDisk cell = new pn_ListCellRendererDisk();
        cell.setProgressValue(Integer.parseInt(values[1]));
        cell.setPath(values[0]);
        cell.setType("[ " + values[2] + " ]");
        cell.setTotal(values[3]);
        cell.setFree(values[4]);
        cell.setUsed(values[5]);
        if(isSelected)
            cell.setSelected();
        return cell;
    }
    
}

